import { _get, _post } from './request.js';

/**
 * 手机号码
 * @param {*} s
 */
function isMobile(s) {
  return /^1[0-9]{10}$/.test(s)
}

// 时间戳转换
function formatDate(value) {
  if (typeof (value) == 'undefined') {
    return ''
  } else {
    let date = new Date(parseInt(value))
    let y = date.getFullYear()
    let MM = date.getMonth() + 1
    MM = MM < 10 ? ('0' + MM) : MM
    let d = date.getDate()
    d = d < 10 ? ('0' + d) : d
    let h = date.getHours()
    h = h < 10 ? ('0' + h) : h
    let m = date.getMinutes()
    m = m < 10 ? ('0' + m) : m
    let s = date.getSeconds()
    s = s < 10 ? ('0' + s) : s
    return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
  }
}
// 手机号中间四位隐藏
function hideFourNum(tel) {
  tel = "" + tel;
  var tel1 = tel.replace(tel.substring(3, 7), "****")
  return tel1
}

// 倒计时转化
function formattime(time) {
  var leave1 = time % (24 * 3600 * 1000) //计算小时    
  var hour = Math.floor(leave1 / (3600 * 1000))
  var leave2 = leave1 % (3600 * 1000) //计算分钟    
  var minute = Math.floor(leave2 / (60 * 1000))
  var leave3 = leave2 % (60 * 1000) //计算秒    
  var second = Math.round(leave3 / 1000) == '60' ? '00' : Math.round(leave3 / 1000)
  let str = { hour: formatNumber(hour), minute: formatNumber(minute), second: formatNumber(second) }
  return str
}
function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}


module.exports = {
  isMobile,
  formattime,
  formatDate,
  hideFourNum,
}